const express = require("express");
const router = express.Router();

function getRandomId() {
  return Math.floor(Math.random() * 1000000);
}

function dataChange(pageNumber = 1, pageSize = 10, data) {
  const startIndex = (Number(pageNumber) - 1) * Number(pageSize);
  const endIndex = startIndex + Number(pageSize);
  const currentData = data.slice(startIndex, endIndex);
  const result = {
    code: 0,
    data: {
      record: currentData,
      pageNumber: +pageNumber,
      pageSize: +pageSize,
      totalItems: data.length,
      totalPages: Math.ceil(data.length / pageSize),
    },
  };
  return result;
}

const studentData = [
  {
    class: "21软工1班",
    number: 199000346,
    name: "王永",
    gender: "男",
    id: 9089500,
  },
  {
    class: "21软工11班",
    number: 219960207,
    name: "付钰",
    gender: "女",
    id: 4533939,
  },
  {
    class: "21软工1班",
    number: 219970101,
    name: "曹欣",
    gender: "女",
    id: 9457159,
  },
  {
    class: "21软工1班",
    number: 219970102,
    name: "陈祖平",
    gender: "男",
    id: 9725520,
  },
  {
    class: "21软工1班",
    number: 219970106,
    name: "何艺",
    gender: "女",
    id: 2290755,
  },
  {
    class: "21软工1班",
    number: 219970108,
    name: "黄蓉",
    gender: "女",
    id: 9746868,
  },
  {
    class: "21软工1班",
    number: 219970115,
    name: "李锐",
    gender: "男",
    id: 530707,
  },
  {
    class: "21软工1班",
    number: 219970118,
    name: "林庆红",
    gender: "女",
    id: 2960508,
  },
  {
    class: "21软工1班",
    number: 219970123,
    name: "罗寒露",
    gender: "男",
    id: 8582311,
  },
  {
    class: "21软工1班",
    number: 219970125,
    name: "秦云",
    gender: "男",
    id: 5078169,
  },
  {
    class: "21软工1班",
    number: 219970127,
    name: "孙清平",
    gender: "男",
    id: 9223550,
  },
  {
    class: "21软工1班",
    number: 219970128,
    name: "谭登峰",
    gender: "男",
    id: 772793,
  },
  {
    class: "21软工1班",
    number: 219970129,
    name: "谭朋伟",
    gender: "男",
    id: 4381688,
  },
  {
    class: "21软工1班",
    number: 219970131,
    name: "田玲",
    gender: "女",
    id: 9769649,
  },
  {
    class: "21软工1班",
    number: 219970133,
    name: "王钦",
    gender: "男",
    id: 6106664,
  },
  {
    class: "21软工1班",
    number: 219970134,
    name: "魏莹莹",
    gender: "女",
    id: 6724910,
  },
  {
    class: "21软工1班",
    number: 219970135,
    name: "温钧凯",
    gender: "男",
    id: 9022823,
  },
  {
    class: "21软工1班",
    number: 219970136,
    name: "谢殷驹",
    gender: "男",
    id: 5885646,
  },
  {
    class: "21软工1班",
    number: 219970140,
    name: "张福森",
    gender: "男",
    id: 8573511,
  },
  {
    class: "21软工1班",
    number: 219970144,
    name: "张婷",
    gender: "女",
    id: 2074914,
  },
  {
    class: "21软工1班",
    number: 219970145,
    name: "赵俊豪",
    gender: "男",
    id: 9181469,
  },
  {
    class: "21软工1班",
    number: 219970146,
    name: "赵骁勇",
    gender: "男",
    id: 6112248,
  },
  {
    class: "21软工11班",
    number: 219970202,
    name: "邓德金",
    gender: "男",
    id: 6876022,
  },
  {
    class: "21软工11班",
    number: 219970203,
    name: "董晨洋",
    gender: "男",
    id: 3090134,
  },
  {
    class: "21软工11班",
    number: 219970209,
    name: "胡立果",
    gender: "男",
    id: 5665480,
  },
  {
    class: "21软工11班",
    number: 219970211,
    name: "蒋雪林",
    gender: "女",
    id: 9886565,
  },
  {
    class: "21软工11班",
    number: 219970215,
    name: "李建",
    gender: "男",
    id: 7029175,
  },
  {
    class: "21软工11班",
    number: 219970220,
    name: "林子皓",
    gender: "男",
    id: 3616334,
  },
  {
    class: "21软工11班",
    number: 219970221,
    name: "刘俐",
    gender: "女",
    id: 9807055,
  },
  {
    class: "21软工11班",
    number: 219970222,
    name: "刘艺娜",
    gender: "女",
    id: 7537711,
  },
  {
    class: "21软工11班",
    number: 219970226,
    name: "马园园",
    gender: "女",
    id: 3464442,
  },
  {
    class: "21软工11班",
    number: 219970227,
    name: "彭小平",
    gender: "女",
    id: 1588334,
  },
  {
    class: "21软工11班",
    number: 219970228,
    name: "冉双",
    gender: "女",
    id: 7161730,
  },
  {
    class: "21软工11班",
    number: 219970230,
    name: "唐家怡",
    gender: "女",
    id: 3147696,
  },
  {
    class: "21软工11班",
    number: 219970233,
    name: "王年英",
    gender: "女",
    id: 7800125,
  },
  {
    class: "21软工11班",
    number: 219970235,
    name: "文雪",
    gender: "女",
    id: 814936,
  },
  {
    class: "21软工11班",
    number: 219970236,
    name: "吴家乐",
    gender: "男",
    id: 9609725,
  },
  {
    class: "21软工11班",
    number: 219970239,
    name: "颜芮",
    gender: "女",
    id: 1947845,
  },
  {
    class: "21软工11班",
    number: 219970241,
    name: "袁榛",
    gender: "女",
    id: 9709700,
  },
  {
    class: "21软工11班",
    number: 219970244,
    name: "张云飞",
    gender: "男",
    id: 4797349,
  },
  {
    class: "21软工11班",
    number: 219970248,
    name: "周双迪",
    gender: "女",
    id: 1383859,
  },
  {
    class: "21软工1班",
    number: 219970405,
    name: "付云天",
    gender: "男",
    id: 1647388,
  },
  {
    class: "21软工1班",
    number: 219970414,
    name: "刘思怡",
    gender: "女",
    id: 9650858,
  },
  {
    class: "21软工1班",
    number: 219970422,
    name: "蓬清霞",
    gender: "女",
    id: 3566996,
  },
  {
    class: "21软工1班",
    number: 219970431,
    name: "吴蕊君",
    gender: "女",
    id: 4833682,
  },
  {
    class: "21软工11班",
    number: 219970512,
    name: "李冰雪",
    gender: "女",
    id: 6368963,
  },
  {
    class: "21软工11班",
    number: 219970514,
    name: "李锦涛",
    gender: "男",
    id: 5857191,
  },
  {
    class: "21软工11班",
    number: 219970532,
    name: "王小慧",
    gender: "女",
    id: 2360138,
  },
  {
    class: "21软工11班",
    number: 219970534,
    name: "熊杰",
    gender: "男",
    id: 9715180,
  },
  {
    class: "21软工11班",
    number: 219970543,
    name: "张睿",
    gender: "男",
    id: 9305373,
  },
  {
    class: "21软工11班",
    number: 219970545,
    name: "钟琳翔",
    gender: "男",
    id: 4621420,
  },
  {
    class: "21软工11班",
    number: 219970547,
    name: "周英",
    gender: "女",
    id: 6037337,
  },
  {
    class: "21软工1班",
    number: 219970801,
    name: "伯健飞",
    gender: "男",
    id: 7730217,
  },
  {
    class: "21软工1班",
    number: 219970803,
    name: "陈家豪",
    gender: "男",
    id: 9468502,
  },
  {
    class: "21软工1班",
    number: 219970804,
    name: "陈星宇",
    gender: "男",
    id: 5489439,
  },
  {
    class: "21软工1班",
    number: 219970810,
    name: "龚道益",
    gender: "男",
    id: 8901203,
  },
  {
    class: "21软工1班",
    number: 219970813,
    name: "黄镐楠",
    gender: "男",
    id: 7162771,
  },
  {
    class: "21软工11班",
    number: 219970818,
    name: "刘佳强",
    gender: "男",
    id: 502212,
  },
  {
    class: "21软工11班",
    number: 219970836,
    name: "王菁菁",
    gender: "女",
    id: 689221,
  },
  {
    class: "21软工1班",
    number: 219971002,
    name: "曹志伟",
    gender: "男",
    id: 6837627,
  },
  {
    class: "21软工1班",
    number: 219971017,
    name: "李雪琴",
    gender: "女",
    id: 4299728,
  },
  {
    class: "21软工1班",
    number: 219971031,
    name: "陶敏",
    gender: "女",
    id: 9052379,
  },
  {
    class: "21软工1班",
    number: 219971043,
    name: "张雯婷",
    gender: "女",
    id: 9823625,
  },
  {
    class: "21软工1班",
    number: 219971045,
    name: "周红娇",
    gender: "女",
    id: 5316988,
  },
  {
    class: "21软工1班",
    number: 219971101,
    name: "陈妲姗",
    gender: "女",
    id: 1447738,
  },
  {
    class: "21软工11班",
    number: 219971105,
    name: "杜冉雪",
    gender: "女",
    id: 4792573,
  },
  {
    class: "21软工11班",
    number: 219971108,
    name: "付相娟",
    gender: "女",
    id: 9829345,
  },
  {
    class: "21软工11班",
    number: 219971109,
    name: "何龙",
    gender: "男",
    id: 5442837,
  },
  {
    class: "21软工11班",
    number: 219971117,
    name: "李志祥",
    gender: "男",
    id: 2530658,
  },
  {
    class: "21软工1班",
    number: 219971119,
    name: "刘小龙",
    gender: "男",
    id: 2280139,
  },
  {
    class: "21软工1班",
    number: 219971127,
    name: "唐彬",
    gender: "男",
    id: 6276478,
  },
  {
    class: "21软工1班",
    number: 219971128,
    name: "唐伽有",
    gender: "男",
    id: 1615460,
  },
  {
    class: "21软工11班",
    number: 219971130,
    name: "韦林鹏",
    gender: "男",
    id: 169747,
  },
  {
    class: "21软工1班",
    number: 219971137,
    name: "杨美婕",
    gender: "女",
    id: 2295363,
  },
  {
    class: "21软工1班",
    number: 219971145,
    name: "赵泽宇",
    gender: "男",
    id: 3436583,
  },
  {
    class: "21软工11班",
    number: 219971146,
    name: "郑洋洋",
    gender: "男",
    id: 1419437,
  },
  {
    class: "21软工11班",
    number: 219971302,
    name: "蔡华鉴",
    gender: "男",
    id: 5601607,
  },
  {
    class: "21软工11班",
    number: 219971313,
    name: "江承宇",
    gender: "男",
    id: 5241956,
  },
  {
    class: "21软工11班",
    number: 219971326,
    name: "卿渝桥",
    gender: "男",
    id: 8638207,
  },
  {
    class: "21软工11班",
    number: 219971333,
    name: "吴倩",
    gender: "女",
    id: 3517432,
  },
  {
    class: "21软工11班",
    number: 219971336,
    name: "杨海天",
    gender: "男",
    id: 4525328,
  },
  {
    class: "21软工11班",
    number: 219971346,
    name: "赵欣意",
    gender: "女",
    id: 7064163,
  },
];

// 查询所有学生
router.get("/students", (req, res) => {
  let resultData = studentData;
  const filters = req.query;
  if (Object.keys(filters).length > 0) {
    resultData = studentData.filter((product) =>
      Object.keys(filters).every((key) => {
        // 没有直接返回true
        if (!product[key]) {
          return true;
        }
        return String(product[key])
          .toLowerCase()
          .includes(String(filters[key]).toLowerCase());
      })
    );
  }
  const data = dataChange(req.query.pageNumber, req.query.pageSize, resultData);
  res.setHeader("Content-Type", "application/json");
  res.send(JSON.stringify(data));
});

// 查询单个学生
router.get("/students/:id", (req, res) => {
  const studentId = parseInt(req.params.id);
  const student = studentData.find((s) => s.id === studentId);
  if (student) {
    res.json(student);
  } else {
    res.status(404).json({ error: "学生未找到" });
  }
});

// 添加学生
router.post("/students", (req, res) => {
  const newStudent = req.body;
  if (
    !newStudent ||
    !newStudent.name ||
    !newStudent.class ||
    !newStudent.number ||
    !newStudent.gender
  ) {
    res.status(400).json({ error: "数据不完整" });
  } else {
    newStudent.id = getRandomId();
    studentData.unshift(newStudent);
    res.json({ message: "学生添加成功", data: newStudent });
  }
});

// 修改学生信息
router.put("/students/:id", (req, res) => {
  const studentId = parseInt(req.params.id);
  const updatedStudent = req.body;
  const index = studentData.findIndex((s) => s.id === studentId);
  if (index !== -1) {
    studentData[index] = { ...studentData[index], ...updatedStudent };
    res.json({ message: "学生信息修改成功", data: studentData[index] });
  } else {
    res.status(404).json({ error: "学生未找到" });
  }
});

// 删除学生
router.delete("/students/:id", (req, res) => {
  const studentId = parseInt(req.params.id);
  const index = studentData.findIndex((s) => s.id === studentId);
  if (index !== -1) {
    const deletedStudent = studentData.splice(index, 1);
    res.json({ message: "学生删除成功", data: deletedStudent });
  } else {
    res.status(404).json({ error: "学生未找到" });
  }
});

module.exports = router;
